/*
 * This file is part of Advanced ABX
 *
 * Copyright 2010 Christian Uebber
 *
 * Advanced ABX is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * Advanced ABX is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Advanced ABX.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.uebber.sound.abx.model;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.File;

/**
 * ABXBean encapsulates the application's UI state, separate from the
 * concrete GUI implementation.
 *
 * @author Christian Uebber
 */
public class ABXBean {

    private int numberOfRounds = 16;
    private int transitionLength = 80;
    private int trackLength;
    private boolean modifiable = true;
    private boolean readyForReading = false;
    private boolean readyForPlayback = false;
    private boolean readyForTesting = false;

    private StringBuffer logBuffer = new StringBuffer();

    private File fileA;
    private File fileB;

    private PropertyChangeSupport changes = new PropertyChangeSupport(this);

    public ABXBean() {
    }

    public int getNumberOfRounds() {
        return numberOfRounds;
    }

    public void setNumberOfRounds(int numberOfRounds) {
        int oldValue = this.numberOfRounds;
        this.numberOfRounds = numberOfRounds;
        changes.firePropertyChange("numberOfRounds", oldValue, numberOfRounds);
    }

    public int getTransitionLength() {
        return transitionLength;
    }

    public void setTransitionLength(int transitionLength) {
        int oldValue = this.transitionLength;
        this.transitionLength = transitionLength;
        changes.firePropertyChange("transitionLength", oldValue, transitionLength);
    }

    public int getTrackMillisecondLength() {
        return trackLength;
    }

    public void setTrackMillisecondLength(int trackLength) {
        int oldValue = this.getTrackMillisecondLength();
        this.trackLength = trackLength;
        changes.firePropertyChange("trackMillisecondLength", oldValue, trackLength);
    }

    public boolean isModifiable() {
        return modifiable;
    }

    public void setModifiable(boolean modifiable) {
        boolean oldValue = this.modifiable;
        this.modifiable = modifiable;
        changes.firePropertyChange("modifiable", oldValue, modifiable);
    }

    public boolean isReadyForTesting() {
        return readyForTesting;
    }

    public void setReadyForTesting(boolean readyForTesting) {
        boolean oldValue = this.readyForTesting;
        this.readyForTesting = readyForTesting;
        changes.firePropertyChange("readyForTesting", oldValue, readyForTesting);
    }

    public String getLog() {
        return logBuffer.toString();
    }

    public void setLog(String log) {
        this.logBuffer = new StringBuffer(log);
    }

    public void publish(String message) {
        changes.firePropertyChange("log", this.logBuffer.toString(), logBuffer.append(message + "\n").toString());
    }

    public boolean isReadyForReading() {
        return readyForReading;
    }

    public void setReadyForReading(boolean readyForReading) {
        boolean oldValue = this.readyForReading;
        this.readyForReading = readyForReading;
        changes.firePropertyChange("readyForReading", oldValue, readyForReading);
    }

    public boolean isReadyForPlayback() {
        return readyForPlayback;
    }

    public void setReadyForPlayback(boolean readyForPlayback) {
        boolean oldValue = this.readyForPlayback;
        this.readyForPlayback = readyForPlayback;
        changes.firePropertyChange("readyForPlayback", oldValue, readyForPlayback);
    }

    public File getFileA() {
        return fileA;
    }

    public String getFileALabel() {
        if (fileA == null)
            return "";
        else
            return fileA.getName();
    }

    public void setFileA(File fileA) {
        File oldFile = this.fileA;
        this.fileA = fileA;
        changes.firePropertyChange("fileA", oldFile, fileA);
    }

    public File getFileB() {
        return fileB;
    }

    public String getFileBLabel() {
        if (fileB == null)
            return "";
        else
            return fileB.getName();
    }

    public void setFileB(File fileB) {
        File oldFile = this.fileB;
        this.fileB = fileB;
        changes.firePropertyChange("fileA", oldFile, fileB);
    }

    public void addPropertyChangeListener(PropertyChangeListener l) {
        changes.addPropertyChangeListener(l);
    }

    public void removePropertyChangeListener(PropertyChangeListener l) {
        changes.removePropertyChangeListener(l);
    }
}
